package com.qingxin.sys.action;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.entity.SysUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.SysPermissionService;
import com.qingxin.sys.service.SysUserRightsService;
import com.qingxin.sys.support.SystemCfg;
import com.qingxin.sys.utils.StringUtils;

/**
 * 用户管理
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/user")
public class UserAction extends BaseAction {
	
	@Autowired
	private SysUserRightsService userRightsService;
	@Autowired
	private SysPermissionService sysPermissionService;

	
	/**
	 * 用户列表
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String list(ModelMap model, HttpServletRequest request){
		LoginUser loginUser = getLoginUser(request);
		if(SystemCfg.isAdminUser(loginUser)){
			return "sys/user/user_list_admin";
		}
		return "sys/user/user_list";
	}
	
	
	/**
	 * 列表数据
	 * @param search
	 * @param user_type
	 * @param user_status
	 * @param regionid
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdata.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(String companyid, String search, PageParam pageParam, HttpServletRequest request){
		LoginUser loginUser = getLoginUser(request);
		if(!SystemCfg.isAdminUser(loginUser)){
			companyid = loginUser.getCompanyid();
		}
		return sysUserService.list(companyid, search, pageParam, request);
	}
	
	
	/**
	 * 添加用户
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add.do", method=RequestMethod.GET)
	public String add(ModelMap model, String leftdata, HttpServletRequest request){
		model.put("companyid", leftdata);
		return "sys/user/user_add";
	}
	
	
	/**
	 * 保存用户信息
	 * @param user
	 * @param result
	 * @param passwd
	 * @param repasswd
	 * @param groupid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method=RequestMethod.POST)
	@ResponseBody
	public Object save(@Valid SysUser user, BindingResult result,
			String passwd, String repasswd, String companyid, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		LoginUser loginUser = getLoginUser(request);
		if(!SystemCfg.isAdminUser(loginUser)){
			user.setCompanyid(loginUser.getCompanyid());
		}
		
		SysError info = sysUserService.add(user, passwd, repasswd, loginUser, request);
		return info;
	}

	
	/**
	 * 修改用户信息
	 * @param model
	 * @param userid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/edit.do", method=RequestMethod.GET)
	public String edit(ModelMap model, String userid, HttpServletRequest request){
		SysUser user = sysUserService.get(userid);
		model.put("user", user);
		return "sys/user/user_edit";
	}
	
	
	/**
	 * 保存修改的用户信息
	 * @param user
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/update.json", method=RequestMethod.POST)
	@ResponseBody
	public Object update(@Valid SysUser user, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		SysError info = sysUserService.update(user, getLoginUser(request), request);
		return info;
	}
	
	
	/**
	 * 查看用户详细详细
	 * @param model
	 * @param userid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/view.do", method=RequestMethod.GET)
	public String view(ModelMap model, String userid, HttpServletRequest request){
		SysUser user = sysUserService.get(userid);
		model.put("user", user);
		return "sys/user/user_view";
		
	}
	
	
	/**
	 * 启用用户账号
	 * @param userid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/enable.json", method=RequestMethod.POST)
	@ResponseBody
	public Object enable(String[] userid, HttpServletRequest request){
		if(userid==null || userid.length==0){
			return new SysError("没有选择任何信息.");
		}
		return sysUserService.updateUser_status(userid, "1", getLoginUser(request), request);
	}
	
	
	/**
	 * 停用用户账号
	 * @param userid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/disable.json", method=RequestMethod.POST)
	@ResponseBody
	public Object disable(String[] userid, HttpServletRequest request){
		if(userid==null || userid.length==0){
			return new SysError("没有选择任何信息.");
		}
		return sysUserService.updateUser_status(userid, "2", getLoginUser(request), request);
	}
	
	
	/**
	 * 删除用户账号
	 * @param userid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.json", method=RequestMethod.POST)
	@ResponseBody
	public Object delete(String[] userid, HttpServletRequest request){
		if(userid==null || userid.length==0){
			return new SysError("没有选择任何信息.");
		}
		return sysUserService.delete(userid, getLoginUser(request), request);
	}
	
	
	/**
	 * 重置密码
	 * @param userid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/resetPassword.json", method=RequestMethod.POST)
	@ResponseBody
	public Object resetPassword(String userid, HttpServletRequest request){
		if(StringUtils.isEmpty(userid)){
			return new SysError("参数错误.");
		}
		return sysUserService.resetPassword(userid, null, getLoginUser(request), request);
	}
	
	
	/**
	 * 判断用户账号是否存在
	 * @param login_name
	 * @return 0不存在、1已存在
	 */
	@RequestMapping(value="/isLoginnameDuplicate.json" , method=RequestMethod.GET)
	@ResponseBody
	public String isLoginnameDuplicate(String login_name){
		SysUser user = sysUserService.getByProperty("login_name", login_name);
		return (user==null) ? "0":"1";
	}
	
	
	/**
	 * 判断用户手机号是否存在
	 * @param phone
	 * @return 0不存在、1已存在
	 */
	@RequestMapping(value="/isPhoneDuplicate.json" , method=RequestMethod.GET)
	@ResponseBody
	public String isPhoneDuplicate(String phone, String userid){
		boolean duplicate = sysUserService.isPhoneDuplicate(phone, userid);
		return duplicate ? "1":"0";
	}
	
	
	/**
	 * 个人信息页面
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/selfInfo.do", method = RequestMethod.GET)
	public String selfInfo(ModelMap model, HttpServletRequest request) {
		LoginUser loginUser = getLoginUser(request);
		SysUser user = sysUserService.get(loginUser.getUserid());
		
		model.put("loginUser", loginUser);
		model.put("user", user);
		
		return "sys/user/self_info";
	}
	
	
	/**
	 * 修改个人信息
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/editSelf.do", method = RequestMethod.GET)
	public String editSelf(ModelMap model, HttpServletRequest request){
		LoginUser loginUser = getLoginUser(request);
		SysUser user = sysUserService.get(loginUser.getUserid());
		
		model.put("loginUser", loginUser);
		model.put("user", user);
		
		return "sys/user/self_edit";
	}
	
	
	/**
	 * 修改个人信息
	 * @param user
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/updateSelf.json" , method=RequestMethod.POST)
	@ResponseBody
	public Object updateSelf(@Valid SysUser user, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		LoginUser loginuser = getLoginUser(request);
		if(loginuser == null || !loginuser.getUserid().equalsIgnoreCase(user.getUserid())){
			return new SysError("参数异常.");
		}
		return sysUserService.updateSelf(user, request);
	}
	
	
	/**
	 * 修改密码
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/updatePassword.do", method = RequestMethod.GET)
	public String updatePassword(ModelMap model, HttpServletRequest request) {
		LoginUser loginUser = getLoginUser(request);
		SysUser user = sysUserService.get(loginUser.getUserid());
		
		model.put("loginUser", loginUser);
		model.put("user", user);
		
		return "sys/user/passwd_edit";
	}
	
	
	/**
	 * 保存修改的密码
	 * @param userid
	 * @param passwd
	 * @param newPasswd
	 * @param newRepasswd
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/updatePassword.json" , method=RequestMethod.POST)
	@ResponseBody
	public Object updatePassword(String userid, String passwd,
			String newPasswd, String newRepasswd, HttpServletRequest request){
		LoginUser loginuser = getLoginUser(request);
		if(loginuser == null || !loginuser.getUserid().equalsIgnoreCase(userid)){
			return new SysError("参数异常.");
		}
		
		return sysUserService.updatePassword(userid, passwd, newPasswd, newRepasswd, request);
	}
	
	
	
	/**
	 * 设置用户权限
	 * @param model
	 * @param userid
	 * @return
	 */
	@RequestMapping(value = "/setUserRights.do", method = RequestMethod.GET)
	public String setUserRights(ModelMap model, String userid) {
		model.put("userid", userid);
		return "sys/user/setUserRights";
	}
	
	
	/**
	 * 获取操作权限树
	 * @param userid
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getPermissions.json", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Object getPermissions(String userid, HttpServletRequest request){
		SysUser user = sysUserService.get(userid);
		if(user == null){
			return new ArrayList<>();
		}
		return sysPermissionService.getUserRightsPermissions(userid, getLoginUser(request));
	}
	
	
	/**
	 * 保存指定用户的操作权限
	 * @param userid
	 * @param perid
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/setUserRights.json", method = RequestMethod.POST)
	@ResponseBody
	public Object setUserRights(String userid, String[] perid, HttpServletRequest request) {
		if(StringUtils.isEmpty(userid)) {
			return new SysError("参数错误.");
		}
		
		return userRightsService.saveUserRights(userid, perid, getLoginUser(request));
	}
	
	
	
}
